package com.sysu.simulation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.sysu.simulation.R;
import com.sysu.simulation.bean.Clothes;

import java.util.HashSet;
import java.util.Set;

public class ClothesListAdapter extends BaseRecyclerAdapter<Clothes>{

    private SparseBooleanArray selectedSet = new SparseBooleanArray();
    private boolean showBox = false;

    public ClothesListAdapter(Context context) {
        super(context,0);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        return new ClothesListViewHolder(mInflater.inflate(R.layout.item_clothes_list, parent, false));
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, Clothes item, int position) {
        ClothesListViewHolder vh = (ClothesListViewHolder) holder;
        //vh.mClothesIndex.setText(Integer.toString(position+1));
        vh.mClothesTitle.setText(item.getTitle());
        vh.mMaterial.setText(item.getMaterial().toString());
        vh.mThickness.setText(item.getThickness().toString());
        vh.mClothesType.setText(item.getType().toString());
        if(showBox) {
            vh.mClothesSelect.setVisibility(View.VISIBLE);
            vh.mClothesSelect.setChecked(selectedSet.get(position,false));
        }
        else {
            vh.mClothesSelect.setVisibility(View.INVISIBLE);
        }
    }

    private class ClothesListViewHolder extends RecyclerView.ViewHolder {
        private TextView mClothesIndex, mClothesTitle, mMaterial, mThickness, mClothesType;
        private CheckBox mClothesSelect;

        public ClothesListViewHolder(View itemView) {
            super(itemView);
            mClothesTitle = (TextView) itemView.findViewById(R.id.clothes_title);
            mClothesIndex = (TextView) itemView.findViewById(R.id.clothes_index);
            mMaterial = (TextView) itemView.findViewById(R.id.material);
            mThickness = (TextView) itemView.findViewById(R.id.thickness);
            mClothesType = (TextView) itemView.findViewById(R.id.clothes_type);
            mClothesSelect = (CheckBox) itemView.findViewById(R.id.clothes_select);
        }
    }

    public void showMultiBox(boolean showBox) {
        this.showBox = showBox;
        if(showBox) selectedSet.clear();
        notifyDataSetChanged();
    }

    public void addOrRemove(int position) {
        if (selectedSet.get(position,false)) {
            selectedSet.delete(position);
        } else {
            selectedSet.put(position,true);
        }
        notifyItemChanged(position);
    }

    public void removeSelectedItems() {
        int offset = 0;
        for(int i=0; i<selectedSet.size(); i++) {
            if(selectedSet.valueAt(i)) {
                mItems.remove(selectedSet.keyAt(i) - offset);
                offset++;
            }
        }
        notifyDataSetChanged();
        //selectedSet.clear();
    }

    public SparseBooleanArray getSelectedSet() {
        return selectedSet;
    }
}
